package controller

import (
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
	"github.com/shopspring/decimal"

	"gitee.com/wangshh_123/cross-pay-go/src/database"
	"gitee.com/wangshh_123/cross-pay-go/src/service"
	"gitee.com/wangshh_123/cross-pay-go/src/utils/common"
)

// BalanceSyncController 余额同步控制器
type BalanceSyncController struct {
	balanceSyncService *service.BalanceSyncService
}

// NewBalanceSyncController 创建余额同步控制器
func NewBalanceSyncController() *BalanceSyncController {
	return &BalanceSyncController{
		balanceSyncService: service.NewBalanceSyncService(database.DB),
	}
}

// GetCardBalance 获取卡片余额
func (c *BalanceSyncController) GetCardBalance(ctx *gin.Context) {
	cardID := ctx.Param("cardId")
	if cardID == "" {
		common.Error(ctx, http.StatusBadRequest, "card_id_required", nil)
		return
	}

	balance, err := c.balanceSyncService.GetCardBalance(ctx, cardID)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "get_card_balance_failed", err)
		return
	}

	common.Success(ctx, gin.H{
		"cardId":  cardID,
		"balance": balance.String(),
	})
}

// UpdateCardBalance 更新卡片余额
func (c *BalanceSyncController) UpdateCardBalance(ctx *gin.Context) {
	cardID := ctx.Param("cardId")
	if cardID == "" {
		common.Error(ctx, http.StatusBadRequest, "card_id_required", nil)
		return
	}

	var req struct {
		Balance string `json:"balance" binding:"required"`
	}

	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_request_body", err)
		return
	}

	balance, err := decimal.NewFromString(req.Balance)
	if err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_balance_format", err)
		return
	}

	err = c.balanceSyncService.SyncCardBalance(ctx, cardID, balance)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "update_card_balance_failed", err)
		return
	}

	common.Success(ctx, gin.H{
		"cardId":  cardID,
		"balance": balance.String(),
		"message": "余额更新成功",
	})
}

// UpdateCardBalanceByUser 根据用户ID更新卡片余额
func (c *BalanceSyncController) UpdateCardBalanceByUser(ctx *gin.Context) {
	cardID := ctx.Param("cardId")
	if cardID == "" {
		common.Error(ctx, http.StatusBadRequest, "card_id_required", nil)
		return
	}

	userIDStr := ctx.Param("userId")
	if userIDStr == "" {
		common.Error(ctx, http.StatusBadRequest, "user_id_required", nil)
		return
	}

	userID, err := strconv.ParseInt(userIDStr, 10, 64)
	if err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_user_id", err)
		return
	}

	var req struct {
		Balance string `json:"balance" binding:"required"`
	}

	if err := ctx.ShouldBindJSON(&req); err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_request_body", err)
		return
	}

	balance, err := decimal.NewFromString(req.Balance)
	if err != nil {
		common.Error(ctx, http.StatusBadRequest, "invalid_balance_format", err)
		return
	}

	err = c.balanceSyncService.UpdateCardBalanceByUser(ctx, userID, cardID, balance)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "update_card_balance_failed", err)
		return
	}

	common.Success(ctx, gin.H{
		"userId":  userID,
		"cardId":  cardID,
		"balance": balance.String(),
		"message": "用户卡片余额更新成功",
	})
}

// ListCardsWithZeroBalance 列出余额为0的卡片
func (c *BalanceSyncController) ListCardsWithZeroBalance(ctx *gin.Context) {
	cards, err := c.balanceSyncService.ListCardsWithZeroBalance(ctx)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "list_cards_failed", err)
		return
	}

	common.Success(ctx, gin.H{
		"cards":   cards,
		"count":   len(cards),
		"message": "查询成功",
	})
}

// ListCardsByPlatform 按平台列出卡片
func (c *BalanceSyncController) ListCardsByPlatform(ctx *gin.Context) {
	platform := ctx.Param("platform")
	if platform == "" {
		common.Error(ctx, http.StatusBadRequest, "platform_required", nil)
		return
	}

	cards, err := c.balanceSyncService.ListCardsByPlatform(ctx, platform)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "list_cards_failed", err)
		return
	}

	common.Success(ctx, gin.H{
		"platform": platform,
		"cards":    cards,
		"count":    len(cards),
		"message":  "查询成功",
	})
}

// GetBalanceSummary 获取余额统计信息
func (c *BalanceSyncController) GetBalanceSummary(ctx *gin.Context) {
	summary, err := c.balanceSyncService.GetBalanceSummary(ctx)
	if err != nil {
		common.Error(ctx, http.StatusInternalServerError, "get_summary_failed", err)
		return
	}

	common.Success(ctx, gin.H{
		"summary": summary,
		"message": "统计信息获取成功",
	})
}
